//
//  BIDSignMeetingDetailViewController.m
//  JuanChengOA
//
//  Created by mal on 2017/7/28.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDSignMeetingDetailViewController.h"
#import "BIDMeetingDetailCell.h"

//签到
static NSString *signURL = @"noticeAndroid!sign.action";

@interface BIDSignMeetingDetailViewController ()<UITableViewDelegate, UITableViewDataSource, UIWebViewDelegate>
{
    NSArray *_dataSourceArr;
    BIDMeetingDetailCell *_tempCell;
    BOOL _isRegister;
    UIView *_footerView;
}
@end

@implementation BIDSignMeetingDetailViewController
@synthesize meetingID;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"会议详情";
    _dataSourceArr = @[];
    _tempCell = [[[NSBundle mainBundle] loadNibNamed:@"BIDMeetingDetailCell" owner:self options:nil] lastObject];
    _tableView.allowsSelection = NO;
    _tableView.tableFooterView = nil;
    [self loadData];
}

- (void)loadData
{
    NSString *strRequestURL = @"";
    NSString *strPost = @"";
    
    strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], signURL];
    strPost = [[NSString alloc] initWithFormat:@"noticeId=%@&userId=%@", meetingID, [AppDelegate getUserId]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            if(responseDic)
            {
                NSNumber *resValue = responseDic[@"isSuccess"];
                if([resValue intValue]==1)
                {
                    responseDic = responseDic[@"noticeReply"];
                    NSString *strTitle = responseDic[@"title"];
                    NSString *strTime = responseDic[@"meetingTime"];
                    NSString *strPlace = responseDic[@"meetingPlace"];
                    NSString *strSeat = responseDic[@"seat"];
                    NSString *strContent = responseDic[@"content"];
                    //
                    _dataSourceArr = @[@{@"title":@"会议主题", @"content":strTitle},
                                       @{@"title":@"会议时间", @"content":strTime},
                                       @{@"title":@"会议地址", @"content":strPlace},
                                       @{@"title":@"我的座次", @"content":strSeat},
                                       @{@"title":@"", @"content":strContent}];
                    _tableView.tableFooterView = [self createFooterView];
                    [_tableView reloadData];
                }
                else
                {
                    [self showAlertView:responseDic[@"msg"] buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
                }
            }
        }
    }];
}

- (UIView *)createFooterView
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, 0)];
    [_footerView setBackgroundColor:[UIColor whiteColor]];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_footerView.frame), 35.f)];
    [label setText:@"    会议内容"];
    [label setFont:[UIFont systemFontOfSize:15.f]];
    [label setTextColor:RGBACOLOR(50, 54, 59, 1)];
    [label setBackgroundColor:RGBACOLOR(241.f, 241.f, 241.f, 1.f)];
    [_footerView addSubview:label];
    //
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[[_dataSourceArr lastObject][@"content"] dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    CGFloat contentHeight = [BIDGlobalMethods getHeightWithAttributeString:attrStr constraintSize:CGSizeMake(CGRectGetWidth(_footerView.frame)-32, MAXFLOAT)];
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetHeight(label.frame)+5, CGRectGetWidth(_footerView.frame)-32, contentHeight)];
    contentLabel.numberOfLines = 0;
    //
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(16, CGRectGetHeight(label.frame)+5, CGRectGetWidth(_footerView.frame)-32, contentHeight)];
    webView.delegate = self;
    [webView loadHTMLString:attrStr.string baseURL:nil];
    CGRect webViewFrame = webView.frame;
    CGFloat webViewHeight = webView.scrollView.contentSize.height;
    webViewFrame.size.height = webViewHeight;
    webView.frame = webViewFrame;
    [_footerView addSubview:webView];
    //
    CGRect footerViewFrame = _footerView.frame;
    footerViewFrame.size.height = webViewHeight + CGRectGetHeight(label.frame) + 10;
    _footerView.frame = footerViewFrame;
    return _footerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    webViewHeight = webView.scrollView.contentSize.height;
    //
    CGRect webViewFrame = webView.frame;
    webViewFrame.size.height = webViewHeight;
    webView.frame = webViewFrame;
    //
    CGRect footerViewFrame = _footerView.frame;
    footerViewFrame.size.height = webViewHeight + 35 + 10;
    _footerView.frame = footerViewFrame;
    _tableView.tableFooterView = _footerView;
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count-1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_isRegister)
    {
        _isRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDMeetingDetailCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDMeetingDetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = _dataSourceArr[row];
    detailCell.titleLabel.text = [[NSString alloc] initWithFormat:@"%@:%@", dic[@"title"], dic[@"content"]];
    cell = detailCell;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0;
    NSDictionary *dic = _dataSourceArr[indexPath.row];
    _tempCell.titleLabel.preferredMaxLayoutWidth = CGRectGetWidth(tableView.frame)-16*2;
    _tempCell.titleLabel.text = [[NSString alloc] initWithFormat:@"%@:%@", dic[@"title"], dic[@"content"]];
    CGSize size = [_tempCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    height = size.height<44 ? 44 : size.height+1;
    return height;
}


@end
